package com.yaolisheng.util.prometheus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Objects;

/**
 * @Company: 神州数码信息系统有限公司
 * @Author : yaols
 * @Date: 2022/10/08/19:20
 **/
public class PromInfoUtils {
  private static final Logger log = LoggerFactory.getLogger(PromInfoUtils.class);
  /**
   * @Title: Windows主机操信息
   * @param promURL
   * @param promQL
   */
  public static List<PromResultInfo> getWindowsInfo(String promURL, String promQL) {
    log.info("【Windows主机信息】，请求地址：{}，请求QL：{}", promURL, promQL);
    JSONObject param = new JSONObject();
    param.put(PromConstants.QUERY, promQL);
    String http = null;
    try {
      http = RestTemplateUtils.getHttp(promURL, param, 10000, 10000, 1);
    } catch (Exception e) {
      log.error("【Windows主机信息】异常，请求地址：{}，请求QL：{}，异常信息：{}", promURL, promQL, e);
    }
    PromResponceInfo responceInfo = JSON.parseObject(http, PromResponceInfo.class);
    log.info("【Windows主机信息】，请求地址：{}，请求QL：{}，返回信息：{}", promURL, promQL, responceInfo);
    if (Objects.isNull(responceInfo)) {
      return null;
    }
    String status = responceInfo.getStatus();
    if (StringUtils.isBlank(status)
            || !PromConstants.SUCCESS.equals(status)
    ) {
      return null;
    }
    List<PromResultInfo> result = responceInfo.getData().getResult();
    return result;
  }
  public static void main(String[] args) {
    //  List<PromResultInfo> windowsOsInfo = getWindowsInfo("http://10.0.50.225:9090/api/v1/query", PromConstants.WINDOWS_OS_INFO);
    List<PromResultInfo> windowsOsInfo = getWindowsInfo("http://10.0.44.50:9090/api/v1/query", PromConstants.WINDOWS_CPU_TIME_TOTAL_IDLE);
    System.out.println(windowsOsInfo);
  }
}
